﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    ViewData["Title"] = "面试管理";
    Layout = null;
}
<link href="~/layui/css/layui.css" rel="stylesheet" />
<script src="~/layui/layui.js"></script>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>面试管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <style>
        body {
            margin: 0;
            padding: 0;
            overflow: hidden;
            font-size: 12px;
        }
        
        .layui-container {
            width: 100% !important;
            max-width: 100% !important;
            padding: 0 10px;
            margin: 0;
        }
        
        .layui-table-cell {
            height: auto !important;
            line-height: 22px;
            padding: 4px 8px;
            white-space: normal;
            font-size: 12px;
        }
        
        .layui-table {
            width: 100%;
            margin: 0;
        }
        
        .layui-table-view {
            margin: 0;
        }
        
        /* 优化表格样式 */
        .layui-table-header th {
            font-weight: 600;
            background-color: #f8f8f8;
            font-size: 12px;
            padding: 4px 8px;
        }
        
        /* 响应式调整 */
        .media screen and (max-width: 1200px) {
            .layui-table-cell {
                padding: 4px 6px;
            }
        }
        
        /* 修复表格溢出 */
        .layui-table-box {
            overflow-x: auto;
        }
        
        /* 修复底部空白问题 */
        html, body {
            height: 100%;
        }
        
        .layui-table-page {
            margin: 0 !important;
            padding: 2px 0 !important;
        }
        
        .layui-laypage {
            margin: 2px 0 !important;
            font-size: 12px;
        }
        
        /* 操作按钮样式优化 */
        .layui-btn-xs {
            padding: 0 6px;
            height: 22px;
            line-height: 22px;
            font-size: 12px;
            margin-right: 2px;
            white-space: nowrap;
        }
        
        .layui-form-label.required:before {
            content: '* ';
            color: red;
            margin-right: 2px;
        }
        
        .layui-input-inline {
            width: 150px !important;
        }
        
        .invite-interview-container .layui-form-item {
            margin-bottom: 10px;
        }
        
        /* 调整搜索区域样式 */
        .layui-form-label {
            padding: 6px 10px;
            font-size: 12px;
        }
        
        .layui-input, .layui-select, .layui-textarea {
            height: 30px;
            line-height: 30px;
            font-size: 12px;
        }
        
        .layui-btn {
            height: 30px;
            line-height: 30px;
            padding: 0 15px;
            font-size: 12px;
        }
        
        .layui-row {
            margin-bottom: 8px;
        }
    </style>
</head>
<body>
    <div class="layui-container" style="margin: 15px;">
        <!-- 搜索区域 -->
        <div class="layui-row layui-form">
            <div class="layui-col-md12">
                <div class="layui-inline">
                    <label class="layui-form-label">人员名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="personnelName" placeholder="请输入人员名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">面试官</label>
                    <div class="layui-input-inline">
                        <input type="text" name="interviewer" placeholder="请输入面试官姓名" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="search">
                        <i class="layui-icon layui-icon-search"></i> 搜索
                    </button>
                    <button class="layui-btn layui-btn-primary" lay-submit lay-filter="reset">
                        <i class="layui-icon layui-icon-refresh"></i> 重置
                    </button>
                </div>
            </div>
        </div>

        <!-- 工具栏 -->
        <div class="layui-row" style="margin-top: 15px;">
            <div class="layui-col-md12">
                <button class="layui-btn layui-btn-normal" id="exportBtn">
                    <i class="layui-icon layui-icon-export"></i> 导出全部
                </button>
                <button class="layui-btn layui-btn-warm layui-btn-disabled" id="exportSelectedBtn" disabled>
                    <i class="layui-icon layui-icon-export"></i> 导出选中
                </button>
            </div>
        </div>

        <!-- 表格 -->
        <table id="interviewTable" lay-filter="interviewTable"></table>
    </div>

    <!-- 操作列模板 -->
    <script type="text/html" id="operationTpl">
        {{#  if(d.interviewResult !== '通过' && d.interviewResult !== '未通过'){ }}
        <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="invite">
            <i class="layui-icon layui-icon-dialogue"></i>邀请面试
        </a>
        {{#  } }}
    </script>

    <!-- 性别模板 -->
    <script type="text/html" id="sexTpl">
        {{#  if(d.sex === 1){ }}
            <span class="layui-badge layui-bg-blue">男</span>
        {{#  } else if(d.sex === 2) { }}
            <span class="layui-badge layui-bg-orange">女</span>
        {{#  } else { }}
            <span class="layui-badge layui-bg-gray">未知</span>
        {{#  } }}
    </script>

    <!-- 面试结果模板 -->
    <script type="text/html" id="resultTpl">
        {{#  if(d.interviewResult === '通过'){ }}
            <span class="layui-badge layui-bg-green">通过</span>
        {{#  } else if(d.interviewResult === '未通过') { }}
            <span class="layui-badge layui-bg-red">未通过</span>
        {{#  } else if(d.interviewResult === '待定') { }}
            <span class="layui-badge layui-bg-orange">待定</span>
        {{#  } else { }}
            <span class="layui-badge layui-bg-gray">{{d.interviewResult}}</span>
        {{#  } }}
    </script>

    <!-- 添加/编辑面试记录弹框模板 -->
    <script type="text/html" id="interviewFormTpl">
        <div class="interview-form-container">
            <form class="layui-form" lay-filter="interviewForm" style="padding: 20px;">
                <input type="hidden" name="id" value="0">
                
                <div class="layui-row">
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label required">人员编码</label>
                            <div class="layui-input-block">
                                <input type="text" name="personnelCode" lay-verify="required" placeholder="请输入人员编码" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label required">人员名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="personnelName" lay-verify="required" placeholder="请输入人员名称" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="layui-row">
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label required">联系电话</label>
                            <div class="layui-input-block">
                                <input type="text" name="phone" lay-verify="required|phone" placeholder="请输入联系电话" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label required">性别</label>
                            <div class="layui-input-block">
                                <input type="radio" name="sex" value="1" title="男" checked>
                                <input type="radio" name="sex" value="2" title="女">
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-row">
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label required">定级</label>
                            <div class="layui-input-block">
                                <input type="text" name="definiteIntegral" lay-verify="required" placeholder="请输入定级" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label required">归属部门</label>
                            <div class="layui-input-block">
                                <input type="text" name="affiliatedDepartment" lay-verify="required" placeholder="请输入归属部门" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-row">
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label required">岗位</label>
                            <div class="layui-input-block">
                                <input type="text" name="postWage" lay-verify="required" placeholder="请输入岗位" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label required">角色</label>
                            <div class="layui-input-block">
                                <input type="text" name="roles" lay-verify="required" placeholder="请输入角色" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-row">
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label required">面试官</label>
                            <div class="layui-input-block">
                                <input type="text" name="interviewer" lay-verify="required" placeholder="请输入面试官" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label required">面试次数</label>
                            <div class="layui-input-block">
                                <input type="number" name="interviewNumber" lay-verify="required|number" placeholder="请输入面试次数" autocomplete="off" class="layui-input" min="1" value="1">
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-row">
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label required">面试时间</label>
                            <div class="layui-input-block">
                                <input type="text" name="interviewsTime" id="interviewsTime" lay-verify="required" placeholder="请选择面试时间" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label required">面试结果</label>
                            <div class="layui-input-block">
                                <select name="interviewResult" lay-verify="required">
                                    <option value="">请选择面试结果</option>
                                    <option value="通过">通过</option>
                                    <option value="未通过">未通过</option>
                                    <option value="待定">待定</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">面试反馈</label>
                    <div class="layui-input-block">
                        <textarea name="interviewFeedback" placeholder="请输入面试反馈" class="layui-textarea"></textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="submitInterviewForm">提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </script>

    <!-- 邀请面试弹框模板 -->
    <script type="text/html" id="inviteInterviewTpl">
        <div class="invite-interview-container">
            <form class="layui-form" lay-filter="inviteInterviewForm" style="padding: 20px;">
                <input type="hidden" name="id" value="0">
                <input type="hidden" name="personnelName" value="">
                
                <div class="layui-form-item">
                    <label class="layui-form-label required">人员编码</label>
                    <div class="layui-input-inline">
                        <input type="text" name="personnelCode" class="layui-input" readonly>
                    </div>
                    
                    <label class="layui-form-label required">人员名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="displayPersonnelName" class="layui-input" readonly>
                    </div>
                </div>
                
                <div class="layui-form-item">
                    <label class="layui-form-label required">归属部门</label>
                    <div class="layui-input-inline">
                        <select name="affiliatedDepartment" lay-verify="required">
                            <option value="">请选择归属部门</option>
                            <option value="技术部">技术部</option>
                            <option value="销售部">销售部</option>
                            <option value="市场部">市场部</option>
                            <option value="人事部">人事部</option>
                            <option value="财务部">财务部</option>
                        </select>
                    </div>
                    
                    <label class="layui-form-label required">岗位</label>
                    <div class="layui-input-inline">
                        <select name="postWage" lay-verify="required">
                            <option value="">请选择岗位</option>
                            <option value="开发工程师">开发工程师</option>
                            <option value="测试工程师">测试工程师</option>
                            <option value="产品经理">产品经理</option>
                            <option value="设计师">设计师</option>
                            <option value="运维工程师">运维工程师</option>
                        </select>
                    </div>
                </div>
                
                <div class="layui-form-item">
                    <label class="layui-form-label required">角色</label>
                    <div class="layui-input-inline">
                        <select name="roles" lay-verify="required">
                            <option value="">请选择角色</option>
                            <option value="普通员工">普通员工</option>
                            <option value="团队负责人">团队负责人</option>
                            <option value="部门经理">部门经理</option>
                            <option value="技术专家">技术专家</option>
                        </select>
                    </div>
                    
                    <label class="layui-form-label required">面试官</label>
                    <div class="layui-input-inline">
                        <select name="interviewer" lay-verify="required">
                            <option value="">请选择面试官</option>
                            <option value="张三">张三</option>
                            <option value="李四">李四</option>
                            <option value="王五">王五</option>
                            <option value="赵六">赵六</option>
                        </select>
                    </div>
                </div>
                
                <div class="layui-form-item">
                    <label class="layui-form-label required">面试时间</label>
                    <div class="layui-input-inline">
                        <input type="text" name="interviewsTime" id="inviteInterviewsTime" lay-verify="required" placeholder="格式：2023-06-18 16:30" autocomplete="off" class="layui-input">
                    </div>
                    
                    <label class="layui-form-label">面试次数</label>
                    <div class="layui-input-inline">
                        <input type="number" name="interviewNumber" value="1" min="1" class="layui-input">
                    </div>
                </div>
                
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">面试反馈</label>
                    <div class="layui-input-block">
                        <textarea name="interviewFeedback" placeholder="请输入面试反馈内容" class="layui-textarea"></textarea>
                    </div>
                </div>
                
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea name="remarks" placeholder="请输入备注信息" class="layui-textarea"></textarea>
                    </div>
                </div>

                <div class="layui-form-item" style="text-align: center; margin-top: 30px;">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="submitInviteForm">确 定</button>
                    <button type="button" class="layui-btn layui-btn-primary" onclick="layer.closeAll()">取 消</button>
                </div>
            </form>
        </div>
    </script>

    <script>
        // 日期格式化辅助函数
        function formatDate(date) {
            return date.getFullYear() + '-' + 
                  (date.getMonth()+1).toString().padStart(2,'0') + '-' + 
                  date.getDate().toString().padStart(2,'0') + ' ' +
                  date.getHours().toString().padStart(2,'0') + ':' +
                  date.getMinutes().toString().padStart(2,'0');
        }
        
        layui.use(['table', 'form', 'layer', 'jquery', 'laydate'], function(){
            var table = layui.table,
                form = layui.form,
                layer = layui.layer,
                $ = layui.jquery,
                laydate = layui.laydate;

            // 动态计算表格高度
            function calculateTableHeight() {
                var windowHeight = $(window).height();
                var searchAreaHeight = $('.layui-row.layui-form').outerHeight(true) || 0;
                var toolbarHeight = $('.layui-row:eq(1)').outerHeight(true) || 0;
                var paginationHeight = 40; // 分页区域的估计高度
                var padding = 30; // 额外的内边距
                
                return windowHeight - searchAreaHeight - toolbarHeight - paginationHeight - padding;
            }
            
            // 监听窗口大小变化
            $(window).resize(function() {
                table.reload('interviewTable', {
                    height: calculateTableHeight()
                });
            });

            // 表格实例
            table.render({
                elem: '#interviewTable',
                url: '/InterviewControl/GetInterviewList',
                method: 'get',
                page: {
                    layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
                    groups: 5
                },
                request: {
                    pageName: 'pageIndex',
                    limitName: 'pageSize'
                },
                cols: [[ // 表头
                    {type: 'checkbox', width: 50},
                    {field: 'personnelCode', title: '人员编码', width: 80},
                    {field: 'personnelName', title: '人员名称', width: 80},
                    {field: 'phone', title: '联系电话', width: 100},
                    {field: 'sex', title: '性别', width: 50, templet: '#sexTpl'},
                    {field: 'definiteIntegral', title: '定级', width: 50},
                    {field: 'affiliatedDepartment', title: '归属部门', width: 90},
                    {field: 'postWage', title: '岗位', width: 80},
                    {field: 'interviewer', title: '面试官', width: 80},
                    {field: 'interviewFeedback', title: '面试反馈', minWidth: 180},
                    {field: 'interviewResult', title: '面试结果', width: 80, templet: '#resultTpl'},
                    {field: 'interviewNumber', title: '面试次数', width: 45},
                    {field: 'interviewsTime', title: '面试时间', width: 130, templet: function(d){
                        // 优化日期显示格式
                        if(!d.interviewsTime) return '';
                        try {
                            var date = new Date(d.interviewsTime);
                            if(!isNaN(date.getTime())) {
                                return date.getFullYear() + '-' + 
                                      (date.getMonth()+1).toString().padStart(2,'0') + '-' + 
                                      date.getDate().toString().padStart(2,'0') + ' ' +
                                      date.getHours().toString().padStart(2,'0') + ':' +
                                      date.getMinutes().toString().padStart(2,'0');
                            }
                            return d.interviewsTime;
                        } catch(e) {
                            return d.interviewsTime;
                        }
                    }},
                    {title: '操作', width: 120, toolbar: '#operationTpl'}
                ]],
                limits: [5, 10, 20, 50],
                limit: 5,
                height: calculateTableHeight() - 40, // 减少高度以适应分页
                done: function() {
                    // 调整表格宽度，使其填充容器
                    $('.layui-table-view').css('width', '100%');
                    
                    // 处理内容溢出
                    $('.layui-table-cell').css({
                        'white-space': 'nowrap',
                        'text-overflow': 'ellipsis'
                    });
                    
                    // 移除底部多余空白
                    $('.layui-table-page').css({
                        'margin': '0',
                        'padding': '2px 0'
                    });
                    
                    // 修复底部空白问题
                    $('html, body').css('height', '100%');
                    $('html, body').css('overflow-y', 'hidden');
                }
            });

            // 监听表格复选框选择
            table.on('checkbox(interviewTable)', function(obj){
                // 获取选中的数据
                var checkStatus = table.checkStatus('interviewTable');
                // 更新导出选中按钮状态
                if(checkStatus.data.length > 0) {
                    $('#exportSelectedBtn').removeClass('layui-btn-disabled').prop('disabled', false);
                } else {
                    $('#exportSelectedBtn').addClass('layui-btn-disabled').prop('disabled', true);
                }
            });

            // 监听搜索
            form.on('submit(search)', function(data){
                table.reload('interviewTable', {
                    where: {
                        personName: data.field.personnelName,
                        interviewer: data.field.interviewer
                    },
                    page: {
                        curr: 1
                    },
                    done: function() {
                        // 重置导出选中按钮状态
                        $('#exportSelectedBtn').addClass('layui-btn-disabled').prop('disabled', true);
                    }
                });
                return false;
            });

            // 监听重置
            form.on('submit(reset)', function(data){
                // 清空搜索框的值
                document.getElementsByName('personnelName')[0].value = '';
                document.getElementsByName('interviewer')[0].value = '';
                
                // 重新加载表格，重置到第一页
                table.reload('interviewTable', {
                    where: {
                        personName: '',
                        interviewer: ''
                    },
                    page: {
                        curr: 1
                    },
                    done: function() {
                        // 重置导出选中按钮状态
                        $('#exportSelectedBtn').addClass('layui-btn-disabled').prop('disabled', true);
                    }
                });
                
                // 重新渲染表单
                form.render();
                return false;
            });

            // 添加按钮的点击事件已移除

            // 批量删除按钮点击事件
            $('#batchDeleteBtn').on('click', function(){
                var checkStatus = table.checkStatus('interviewTable');
                var data = checkStatus.data;
                if(data.length === 0){
                    layer.msg('请至少选择一条记录', {icon: 2});
                    return;
                }

                layer.confirm('确定要删除选中的' + data.length + '条记录吗？', {icon: 3, title:'提示'}, function(index){
                    var ids = data.map(function(item){ return item.id; });
                    
                    // 发送批量删除请求
                    $.ajax({
                        url: '/InterviewControl/BatchDeleteInterview',
                        type: 'post',
                        contentType: 'application/json',
                        data: JSON.stringify(ids),
                        success: function(res){
                            if(res.code === 0){
                                layer.msg(res.msg, {icon: 1});
                                // 刷新表格
                                table.reload('interviewTable');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        },
                        error: function(){
                            layer.msg('服务器异常，请稍后重试', {icon: 2});
                        }
                    });
                    
                    layer.close(index);
                });
            });

            // 监听工具条事件
            table.on('tool(interviewTable)', function(obj){
                var data = obj.data;
                if(obj.event === 'invite'){
                    // 打开邀请面试弹框
                    openInviteInterviewForm(data);
                }
            });

            // 监听表单提交
            form.on('submit(submitInterviewForm)', function(data){
                var formData = data.field;
                
                // 验证必填字段
                var requiredFields = {
                    'personnelCode': '人员编码',
                    'personnelName': '人员名称',
                    'phone': '联系电话',
                    'definiteIntegral': '定级',
                    'affiliatedDepartment': '归属部门',
                    'postWage': '岗位',
                    'roles': '角色',
                    'interviewer': '面试官',
                    'interviewsTime': '面试时间',
                    'interviewResult': '面试结果'
                };
                
                for (var field in requiredFields) {
                    if (!formData[field]) {
                        layer.msg(requiredFields[field] + '不能为空', {icon: 2});
                        return false;
                    }
                }
                
               
                // 确定是添加还是更新
                var url = formData.id > 0 ? '/InterviewControl/UpdateInterview' : '/InterviewControl/AddInterview';
                
                // 显示加载中
                var loading = layer.msg('正在处理...', {icon: 16, time: 0, shade: 0.3});
                
                // 处理可能为空的参数
                Object.keys(formData).forEach(function(key) {
                    if (formData[key] === undefined || formData[key] === null) {
                        formData[key] = '';
                    }
                });
                
                // 提交表单
                $.ajax({
                    url: url,
                    type: 'post',
                    contentType: 'application/json',
                    data: JSON.stringify(formData),
                    success: function(res){
                        layer.close(loading);
                        if(res.code === 0){
                            layer.msg(res.msg, {icon: 1});
                            layer.closeAll('page'); // 关闭弹窗
                            // 刷新表格
                            table.reload('interviewTable');
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    },
                    error: function(xhr){
                        layer.close(loading);
                        // 尝试解析错误信息
                        try {
                            var errorResponse = JSON.parse(xhr.responseText);
                            layer.msg(errorResponse.msg || '服务器异常，请稍后重试', {icon: 2});
                        } catch(e) {
                            layer.msg('服务器异常，请稍后重试', {icon: 2});
                        }
                    }
                });
                
                return false; // 阻止表单跳转
            });

            // 打开面试表单
            function openInterviewForm(data) {
                // 弹窗标题
                var title = data ? '编辑面试记录' : '添加面试记录';
                
                // 打开弹窗
                layer.open({
                    type: 1,
                    title: title,
                    area: ['800px', '700px'],
                    content: $('#interviewFormTpl').html(),
                    success: function(layero, index){
                        
                        
                        // 如果是编辑，填充表单数据
                        if(data){
                            // 创建一个数据副本，以避免修改原始数据
                            var formData = JSON.parse(JSON.stringify(data));
                            
                           
                            
                            // 设置表单值
                            form.val('interviewForm', formData);
                        } else {
                            // 新增模式下设置默认值
                            // 生成人员编码：STAFF + 年月日 + 4位随机数
                            var now = new Date();
                            var year = now.getFullYear();
                            var month = (now.getMonth() + 1).toString().padStart(2, '0');
                            var day = now.getDate().toString().padStart(2, '0');
                            var randomNum = Math.floor(1000 + Math.random() * 9000); // 生成1000-9999之间的随机数
                            var personnelCode = 'STAFF' + year + month + day + randomNum;
                            
                           
                            
                            form.val('interviewForm', {
                                interviewNumber: "",
                                personnelCode: personnelCode,
                                interviewsTime: ""
                            });
                        }
                        
                        // 重新渲染表单
                        form.render();
                    }
                });
            }

            // 打开邀请面试表单
            function openInviteInterviewForm(data) {
                // 打开弹窗
                layer.open({
                    type: 1,
                    title: '邀请面试',
                    area: ['700px', '600px'], // 调整弹窗大小
                    content: $('#inviteInterviewTpl').html(),
                    success: function(layero, index){
                        // 不再使用日期选择器，直接使用文本输入
                        
                        // 创建一个数据副本，以避免修改原始数据
                        var formData = {
                            id: data.id,
                            personnelCode: data.personnelCode,
                            personnelName: data.personnelName,
                            displayPersonnelName: data.personnelName,
                            // 如果钱八的面试次数是2，那么这里设置为3
                            interviewNumber: data.interviewNumber ? (parseInt(data.interviewNumber) + 1).toString() : "3",
                            // 设置默认面试时间为当前时间
                            interviewsTime: formatDate(new Date()),
                            // 如果已有值，则使用原有值
                            affiliatedDepartment: data.affiliatedDepartment || '',
                            postWage: data.postWage || '',
                            interviewer: data.interviewer || '',
                            roles: data.roles || ''
                        };
                        
                        // 设置表单值
                        form.val('inviteInterviewForm', formData);
                        
                        // 重新渲染表单
                        form.render();
                    }
                });
            }

            // 监听邀请面试表单提交
            form.on('submit(submitInviteForm)', function(data){
                var formData = data.field;
                
                // 确认发送邀请
                layer.confirm('确定要向 ' + formData.displayPersonnelName + ' 发送面试邀请吗？', {icon: 3, title:'确认'}, function(index){
                    layer.close(index);
                    
                    // 显示加载中
                    var loading = layer.msg('正在发送邀请...', {icon: 16, time: 0, shade: 0.3});
                    
                    
                    
                    // 创建邀请数据对象
                    var interviewData = {
                        personnelCode: formData.personnelCode || '',
                        personnelName: formData.personnelName || '',
                        interviewer: formData.interviewer || '',
                        affiliatedDepartment: formData.affiliatedDepartment || '',
                        postWage: formData.postWage || '',
                        // 直接使用表单中的时间字符串
                        interviewsTime: formData.interviewsTime || '',
                        sex: 1, // 默认为男性，可以根据实际情况修改
                        interviewNumber: parseInt(formData.interviewNumber) || 1, // 使用表单中的值，默认为1
                        interviewFeedback: formData.interviewFeedback || '',
                        roles: formData.roles || '',
                        remarks: formData.remarks || '',
                        definiteIntegral: formData.definiteIntegral || '',
                        phone: formData.phone || '',
                        interviewResult: '通过' // 新邀请的面试状态设为已通过
                    };
                    
                    // 发送创建面试邀请请求
                    $.ajax({
                        url: '/InterviewControl/SendInterviewInvitation',
                        type: 'post',
                        contentType: 'application/json',
                        data: JSON.stringify(interviewData),
                        success: function(res){
                            layer.close(loading);
                            if(res.code === 0){
                                layer.msg(res.msg, {icon: 1});
                                layer.closeAll('page'); // 关闭弹窗
                                // 刷新表格
                                table.reload('interviewTable');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        },
                        error: function(){
                            layer.close(loading);
                            layer.msg('服务器异常，请稍后重试', {icon: 2});
                        }
                    });
                });
                
                return false; // 阻止表单跳转
            });

            // 导出Excel按钮事件
            $('#exportBtn').on('click', function(){
                var loading = layer.msg('正在导出，请稍候...', {icon: 16, time: 0, shade: 0.3});
                
                // 构建导出参数
                var personnelName = $('input[name="personnelName"]').val() || '';
                var interviewer = $('input[name="interviewer"]').val() || '';
                
                // 构建导出URL
                var url = '/InterviewControl/ExportToExcel?personnelName=' + encodeURIComponent(personnelName) + '&interviewer=' + encodeURIComponent(interviewer);
                
                // 直接使用window.location.href导航到导出URL
                window.location.href = url;
                
                // 延迟关闭加载层
                setTimeout(function() {
                    layer.close(loading);
                }, 1000);
            });
            
            // 导出选中记录按钮事件
            $('#exportSelectedBtn').on('click', function(){
                // 检查按钮是否禁用
                if($(this).hasClass('layui-btn-disabled')) {
                    return;
                }
                
                var checkStatus = table.checkStatus('interviewTable');
                var data = checkStatus.data;
                if(data.length === 0){
                    layer.msg('请至少选择一条记录', {icon: 2});
                    return;
                }
                
                var loading = layer.msg('正在导出选中记录，请稍候...', {icon: 16, time: 0, shade: 0.3});
                
                // 获取选中记录的ID
                var ids = data.map(function(item){ return item.id; });
                
                // 发送请求导出选中记录
                $.ajax({
                    url: '/InterviewControl/ExportSelectedToExcel',
                    type: 'post',
                    contentType: 'application/json',
                    data: JSON.stringify(ids),
                    xhrFields: {
                        responseType: 'blob' // 指定响应类型为blob
                    },
                    success: function(blob){
                        layer.close(loading);
                        
                        // 创建下载链接
                        var link = document.createElement('a');
                        link.href = window.URL.createObjectURL(blob);
                        link.download = '面试管理选中记录_' + new Date().getTime() + '.xlsx';
                        link.click();
                        
                        // 释放URL对象
                        window.URL.revokeObjectURL(link.href);
                    },
                    error: function(xhr){
                        layer.close(loading);
                        
                        // 尝试解析错误信息
                        try {
                            var errorMsg = JSON.parse(xhr.responseText);
                            layer.msg(errorMsg.msg || '导出失败', {icon: 2});
                        } catch(e) {
                            layer.msg('导出失败：服务器异常', {icon: 2});
                        }
                    }
                });
            });
        });
    </script>
</body>
</html>